//store
import { IRootState } from "@/store/types";
import { Module } from "vuex";
import {
  deletePageData,
  PostPageListData,
  createPageData,
  editPageData,
} from "@/service/main/system/system";

export interface Isystem {
  usersList: any[];
  usersCount: number;
  roleList: any[];
  roleCount: number;
  goodsList: any[];
  goodsCount: number;
  menuList: any[];
  menuCount: number;
}

const systemModule: Module<Isystem, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0,
    };
  },
  mutations: {
    changeUsersList(state, userList) {
      state.usersList = userList;
    },
    changeUsersCount(state, userCount) {
      state.usersCount = userCount;
    },
    changeRoleList(state, List) {
      state.roleList = List;
    },
    changeRoleCount(state, count) {
      state.roleCount = count;
    },
    changeGoodsList(state, List) {
      state.goodsList = List;
    },
    changeGoodsCount(state, count) {
      state.goodsCount = count;
    },
    changeMenuList(state, List) {
      state.menuList = List;
    },
    changeMenuCount(state, count) {
      state.menuCount = count;
    },
  },
  getters: {
    PageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`];
      };
    },
    PageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`];
      };
    },
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      // 1.获取pageUrl
      const pageName = payload.pageName;
      const pageUrl = `/${pageName}/list`;

      // // 2.对页面发送请求
      const pageResult = await PostPageListData(pageUrl, payload.queryInfo);
      // // 3.将数据存储到state中
      const { list, totalCount } = pageResult.data;

      const changePageName =
        pageName.slice(0, 1).toUpperCase() + pageName.slice(1);

      commit(`change${changePageName}List`, list);
      commit(`change${changePageName}Count`, totalCount);
    },
    async deletePageDataActions({ dispatch }, payload: any) {
      const { pageName, id } = payload;
      const pageUrl = `/${pageName}/${id}`;
      await deletePageData(pageUrl);
      dispatch("getPageListAction", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      });
    },
    //modal确认取消逻辑
    async createPageDataActions({ dispatch }, payload) {
      const { pageName, createData } = payload;
      const pageUrl = `/${pageName}`;
      //调用请求函数
      await createPageData(pageUrl, createData);
      dispatch("getPageListAction", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      });
    },
    //编辑
    async editPageDataActions({ dispatch }, payload) {
      const { pageName, editData,id } = payload;
      const pageUrl = `/${pageName}/${id}`;
      //调用请求函数
      await editPageData(pageUrl, editData);
      dispatch("getPageListAction", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      });
    },
  },
};

export default systemModule;
